package com.flainsky.nirvanaplayer.globe;

import java.util.Properties;

import com.flainsky.nirvanaplayer.R;

import android.content.Context;
import android.content.SharedPreferences;

public final class Env extends Properties {
	private static final long serialVersionUID = 7758894385094714377L;
	private static final String SP_NAME = "SERVICE_ADDR";
	private static final String SERVER_NAME = "SERVER_ADDR";
	private static final String URL = "url";
	private static final String ADDR = "address";
	private static Env instance;
	private static Context mContext;
	
	private static String serviceAddress = "";
	private static String serverAddress = "";

	public static Env getInstance() {
		if(instance!=null){
			return instance;
		}
		else{
			makeInstance(mContext);
			return instance;
		}
	}
	public static synchronized void makeInstance(Context context){
		mContext = context;
		if(instance==null){
			instance=new Env();
		}
	}
	private Env(){
		//InputStream is=getClass().getResourceAsStream("\\db.properties");
/*		try{
			//InputStream is = ClassLoader.getSystemResourceAsStream("\\db.properties");
			String userDir = System.getProperty("user.dir");  
			InputStream is = new BufferedInputStream(new FileInputStream(userDir +"\\d.properties"));
			load(is);
		} catch(Exception ce){
			System.out.println("read config file failed！please make sure that the db.properties file exits");
		}*/
		serviceAddress = getServiceSharedPreference();
		serverAddress = getServerSharedPreference();
	}
	public String getValue(int resId)
	{
		return mContext.getString(resId);
	}
	
	public void setServiceAddress(String address)
	{
		serviceAddress = address;
	}
	
	public String getServiceAddress()
	{
		if(serviceAddress == null||serviceAddress.equals(""))
		{
			return mContext.getString(R.string.service);
		}
		else
		{
			return serviceAddress;
		}
	}
	
	public String getServiceSharedPreference()
	{
		SharedPreferences serviceInfo = mContext.getSharedPreferences(SP_NAME, 0);
		String serviceAddr = serviceInfo.getString(URL, mContext.getString(R.string.service));
		return serviceAddr;
	}
	
	public void setServiceSharedPreference(String url)
	{
		SharedPreferences serviceInfo = mContext.getSharedPreferences(SP_NAME, 0);
		serviceInfo.edit().putString(URL, url).commit();
		setServiceAddress(url);
	}
	
	public void setServerAddress(String address)
	{
		serverAddress = address;
	}
	
	public String getServerAddress()
	{
		if(serverAddress == null||serverAddress.equals(""))
		{
			return mContext.getString(R.string.serviceaddr);
		}
		else
		{
			return serverAddress;
		}
	}
	
	public String getServerSharedPreference()
	{
		SharedPreferences serviceInfo = mContext.getSharedPreferences(SERVER_NAME, 0);
		String serviceAddr = serviceInfo.getString(ADDR, mContext.getString(R.string.serviceaddr));
		return serviceAddr;
	}
	
	public void setServerSharedPreference(String address)
	{
		SharedPreferences serviceInfo = mContext.getSharedPreferences(SERVER_NAME, 0);
		serviceInfo.edit().putString(ADDR, address).commit();
		setServerAddress(address);
	}
}
